package com.sniff.atp.modules.test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.OpsDeleteSku;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.service.PublishPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  发布计划
 * </p>
 *
 * @author macro
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/test/publishPlan")
@Api(value = "发布计划", tags = "发布计划")
public class PublishPlanController {


    @Autowired
    private PublishPlanService publishPlanService;


    @ApiOperation("新增发布计划")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PublishPlan publishPlan) {
        boolean success = publishPlanService.create(publishPlan);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改发布计划")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody PublishPlan publishPlan) {

        boolean success = publishPlanService.update(publishPlan.getPublishPlanId(), publishPlan);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PublishPlan> getItem(@PathVariable Long id) {
        PublishPlan publishPlan = publishPlanService.getById(id);
        return CommonResult.success(publishPlan);
    }

    @ApiOperation("根据ID删除发布计划")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = publishPlanService.removeById(id);

        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询发布计划")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<PublishPlan>> list(@RequestBody PublishPlan publishPlan) {
        if(publishPlan.getPageSize() == null){
            publishPlan.setPageSize(10);
        }
        if(publishPlan.getPageNum() == null){
            publishPlan.setPageNum(1);
        }
        Page<PublishPlan> menuList = publishPlanService.list(publishPlan, publishPlan.getPageSize(), publishPlan.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }
}

